package cn.hiapi.admin.api;

import cn.hiapi.basic.ResponseEntity;
import cn.hiapi.basic.controller.BasicController;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.shopkeeper.entity.activity.Activity;
import cn.hiapi.shopkeeper.service.activity.ActivityService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author AdinZ
 * @date 2023/3/2 10:23
 */
@RestController@RequestMapping("/public/test-activity")
public class ActivityTestController extends BasicController {

    private final DispatchContext dispatchContext;

    public ActivityTestController(DispatchContext dispatchContext) {
        this.dispatchContext = dispatchContext;

    }

    @GetMapping("/activity-end")
    public ResponseEntity activityEnd(@RequestParam("id")long id){
        Activity activity = this.dispatchContext.getServiceOne(ActivityService.class).get(id);
        if(activity==null)return toError("活动已结束");
        if(activity.getEndTime()<System.currentTimeMillis())return toError("活动已结束");
        if(activity.getStatus()!=1){
            return toError("活动未开始");
        }
        activity.setEndTime(System.currentTimeMillis());
        activity.setUpdated(System.currentTimeMillis()  );

        this.dispatchContext.getServiceOne(ActivityService.class).save(activity);
        return toSuccess();
    }

}
